#!/usr/bin/perl

use Switch;
use MIME::Base64;

@INVITE_ARRAY = (
	"~hoebus\@61.30.73.24",
	"~hoebus\@socket.csie.chu.edu.tw",
	"~pth\@140.109.53.182",
	"einstein\@FreeBSD.cis.nctu.edu.tw",
	"~wise\@Jobs.mi.chu.edu.tw",
	"laving\@www.iswii.net",
	"[~Memphis\@a123-112.staff.chu.edu.tw",
	"~joehorn\@163.18.24.75",
	"liming\@LEO.csie.chu.edu.tw",
	"~frei\@.*.HINET-IP.hinet.net",
	"~ziway\@mi-235.mi.chu.edu.tw",
	);

sub LOG
{
    my $row = join(" ", @_);
    open(FILE, ">>bot.log");
    printf(FILE "%s\n", $row);
    close(FILE);
}

sub privmsg {
    my $channel = shift;
    my $what = shift;
    my @output = ("privmsg");

    push(@output, encode_base64($channel,""));
    push(@output, encode_base64($what,""));
    printf("%s\n", join(",", @output));
}

sub invite {
    my $channel = shift;
    my $what = shift;
    my @output = ("invite");

    push(@output, encode_base64($channel,""));
    push(@output, encode_base64($what,""));
    printf("%s\n", join(",", @output));
}

sub irc_msg {
    my $who = shift;
    my $channel = shift;
    my $what = shift;

    $who =~m/^(.*?)!(.*?)$/;
    my $nick = $1;
    my $idendify = $2;

    LOG("irc_msg: who(%s) channel(%s) what(%s)\n", $who, $channel, $what);
    printf("idendify(%s)\n", $idendify);

    if ( $what =~m/invite me to (#.*)/ )
    {
	my $invite_channel = $1;

	printf("invite_channel(%s)\n", $invite_channel);

	foreach $check_who ( @INVITE_ARRAY )
	{
	    if ( $idendify == $check_who )
	    {
		printf("invite_channel(%s)\n", $invite_channel);
		invite($nick, $invite_channel);
		return;
	    }
	}

	privmsg($nick, "you($idendify) have no permit to join this channel($invite_channel).");
    }
    elsif ( $what =~m/^say: (.*)$/ )
    {
	my $bot_say = $1;
	privmsg($nick, "$bot_say");
    }
    elsif ( $what =~m/^(#.*): (.*)$/ )
    {
	my $target_channel = $1;
	my $bot_say = $2;
	privmsg($target_channel, $bot_say);
    }
}

sub irc_public {
    my $who = shift;
    my $channel = shift;
    my $what = shift;
    my @output = ("privmsg");

    $who =~m/^(.*?)!~(.*?)$/;
    my $nick = $1;
    my $idendify = $2;

    printf("irc_public: who(%s) channel(%s) what(%s)\n", $who, $channel, $what);

    if ( $what =~m/hi bot/ )
    {
	privmsg($channel, "hello, $nick");
    }
}

my $encoded = $ARGV[0];
my $decoded = decode_base64($encoded);

printf("encoded(%s)\n", $encoded);
printf("decoded(%s)\n", $decoded);

my @message_array = split(",", $decoded);

switch ($message_array[0])
{
    case "irc_public" {
	my $who = decode_base64($message_array[1]);
	my $channel = decode_base64($message_array[2]);
	my $what = decode_base64($message_array[3]);

	printf("message_parser: who(%s) channel(%s) what(%s)\n", $who, $channel, $what);

	irc_public($who, $channel, $what);
    }

    case "irc_msg" {
	my $who = decode_base64($message_array[1]);
	my $channel = decode_base64($message_array[2]);
	my $what = decode_base64($message_array[3]);
	printf("message_parser: irc_msg: %s\n", $decoded);
	printf("message_parser: irc_msg: who(%s) channel(%s) what(%s)\n", $who, $channel, $what);

	irc_msg($who, $channel, $what);
    }

    case "irc_mode" {
	printf("irc_mode: %s\n", $decoded);
    }

    else {
	printf("decoded(%s)\n", $decoded);
    }
}



exit(0);
